﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using SmallBusinessKit.DataAccess.Infrastructure;
using SmallBusinessKit.DataAccess.Interfaces;
using SmallBusinessKit.Models.Entities;

namespace SmallBusinessKit.DataAccess.Repositories
{
    public class XmlNewsRepository : XmlRepositoryBase<News>, INewsRepository
    {
        public XmlNewsRepository(IList<News> data) : base(data) {}
        
        public override News GetById(string id)
        {
            return (DataList.FirstOrDefault(i => i.Id == id));
        }

        public override IEnumerable<News> GetMany(Expression<Func<News, bool>> clause)
        {
            // overriding the ancestor because we want to make a *copy* of List<News>
            // if we need to modify it's contents without altering the original instance.
            var condition = clause.Compile();

            var list = DataList.Where(condition);

            var results = list.Select(i =>
                                            new News
                                            {
                                                Id           = i.Id,
                                                IsVisible    = i.IsVisible,
                                                Title        = i.Title,
                                                Date         = i.Date,
                                                Content      = i.Content,
                                                ImageUrl     = i.ImageUrl,
                                                ImageAltText = i.ImageAltText
                                            }).ToList();
            return (results);
        }
    }    
}
